$(function(){
    var ajaxFill = new Z.AjaxFill({
        url: Z.url('/backend/photo/records'),
        templates: {
            list: $('#list-template'),
            methods: {
                thumbFile: function(file){
                    return file.replace(/\.(jpg|jpeg|png|bmp|gif)$/,'_thumb.$1');
                }
            }
        },
        containers:{
            list: $('ul.records'),
            pageLinks: $('div.page-links')
        },
        data:{
            album:$('select[name=album]').val(),
            pageSize : 10
        },
        after: function(){
            new Z.LightBox($('ul.records li.photo a.show'), []);
        }
    });
    
    ajaxFill.request();

    //改变相册 加载图片
    $('select[name=album]').change(function(){
        ajaxFill.request({
            album:$(this).val()
        });
    });

    //鼠标移到图片区 显示操作链接
    $('li.photo').live('mouseenter',function(){
        $(this).find('div.action').show();
    });
    $('li.photo').live('mouseleave',function(){
        $(this).find('div.action').hide();
    });

    $('a.add').click(function(){
        Z.form({
            width: 500,
            content: $('#form-template').tmpl({action:'insert'}),
            success: function(){
                ajaxFill.reload();
            }
        });
        return false;
    });

    //修改图片标签
    $('a.edit').live('click',function(){
        var id = Z.parseInt($(this).parent().prev().find(':checkbox').val());
        Z.ajax({
            url: Z.url('/backend/photo/find/'+id),
            success: function(data){
                data.action = 'update';
                Z.form({
                    width: 500,
                    content: $('#form-template').tmpl(data),
                    success: function(){
                        ajaxFill.reload();
                    },
                    onOpen: function(dialog){
                        dialog.find('select[name=album]').val(data.album);
                    }
                });
            }
        });
        return false;
    });
    
    //删除单个图片
    $('a.delete').live('click',function(){
        var id = Z.parseInt($(this).parents('li.photo').find('div.label input').val());
        Z.confirm({
            content: '确定要删除吗？',
            onConfirm: function(){
                Z.ajax({
                    url: Z.url('/backend/photo/delete'),
                    data: {
                        id:id
                    },
                    success: function(){
                        ajaxFill.reload();
                    }
                });
            }
        });
        return false;
    });
    
    $('a.select-all').click(function(){
        $('ul.records li.photo div.label input:not(:checked)').attr('checked','ckecked');
    });
    $('a.delete-batch').click(function(){
        var ids = [];
        $('ul.records li.photo div.label input:checked').each(function(){
            ids.push($(this).val());
        });
        if(ids.length){
            Z.confirm({
                content: '确定要批量删除吗?',
                onConfirm: function(){
                    Z.ajax({
                        url: Z.url('/backend/photo/delete',true),
                        data: {
                            id:ids.join(',')
                        },
                        success: function(){
                            ajaxFill.reload();
                        }
                    });
                }
            });
        }else{
            Z.alert('您还没有选择图片');
        }
        
        return false;
    });
    
    window.uploadCallback = function(status,msg,data){
        switch(status){
            case 'XT001':
                if(msg!=''){
                    Z.alert(msg);
                }
                $('ul.tab a.records').trigger('click');
                ajaxFill.reload();
                break;
            case 'XT002':
                Z.alert(msg||'操作失败');
                break;
            case 'XT003':
                Z.alert('您不具备此操作权限');
                break;
            case 'XT004':
                Z.alert('请先登录');
                break;
            default:
                Z.alert('未知错误!');
                  
        };
    };
    
    window.imageError = function(img){
        //如果缩略图没有 就放大图 大图也没有就放404图片
        var src = $(img).attr('src');
        if(src.match(/_thumb\.(jpg|jpeg|png|gif|bmp)$/g)){
            src = src.replace(/_thumb\.(jpg|jpeg|png|gif|bmp)$/g,'.$1');
        }else{
            src = '/assets/img/404.jpg';
        }
        $(img).attr('src',Z.url(src));
    };
});